package com.song.cmsservice.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.song.cmsservice.entity.UcenterMember;
import com.song.cmsservice.entity.vo.RegisterVo;
import com.song.cmsservice.mapper.UcenterMemberMapper;
import com.song.cmsservice.service.UcenterMemberService;
import com.song.commonutils.JwtUtils;
import com.song.commonutils.MD5;

import com.song.commonutils.ResultObj;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;

/**
 * <p>
 * 会员表 服务实现类
 * </p>
 *
 * @author song
 * @since 2020-04-04
 */
@Service
public class UcenterMemberServiceImpl extends ServiceImpl<UcenterMemberMapper, UcenterMember> implements UcenterMemberService {

    @Autowired
    private RedisTemplate<String, String> redisTemplate;

    @Override
    public Object login(UcenterMember ucenterMember) {
        String mobile = ucenterMember.getMobile();
        String jwtToken = null;
        String password = ucenterMember.getPassword();
        if (mobile != null || password != null) {
            //判断手机号
            QueryWrapper<UcenterMember> qw = new QueryWrapper<>();
            qw.eq("mobile", mobile);
            UcenterMember mobileMember = baseMapper.selectOne(qw);
            if (mobileMember == null) {
                return ResultObj.falseObj("登陆失败");
            }
            //判断密码
            //MD5加密
            if (!MD5.encrypt(password).equals(mobileMember.getPassword())) {
                return ResultObj.falseObj("密码错误");
            }
            //判断是否禁用
            if (mobileMember.getIsDisabled()) {
                return ResultObj.falseObj("该用户被禁用");
            }
            //成功   生成token字符串
            jwtToken = JwtUtils.getJwtToken(mobileMember.getId(), mobileMember.getNickname());
        }
        return jwtToken;
    }

    @Override
    public ResultObj register(RegisterVo registerVo) {
        //获取注册数据
        String code = registerVo.getCode();
        String mobile = registerVo.getMobile();
        String nickname = registerVo.getNickname();
        String password = registerVo.getPassword();
        if (code == null || mobile == null || nickname == null || password == null) {
            return ResultObj.falseObj("不能为空");
        }
        String phone = redisTemplate.opsForValue().get(mobile);
        if (!code.equals(phone)) {
            return ResultObj.falseObj("验证码错误");
        }
        //判断手机号是否重复，表里面存在相同手机号不进行添加
        QueryWrapper<UcenterMember> wrapper = new QueryWrapper<>();
        wrapper.eq("mobile", mobile);
        Integer count = baseMapper.selectCount(wrapper);
        if (count > 0) {
            return ResultObj.falseObj("该手机号已注册");
        }
        UcenterMember ucenterMember = new UcenterMember();
        ucenterMember.setMobile(mobile);
        ucenterMember.setNickname(nickname);
        ucenterMember.setPassword(MD5.encrypt(password));
        ucenterMember.setAvatar("https://edu-t.oss-cn-beijing.aliyuncs.com/2020/03/28/b7409e42aa054c3b9a94f8662e9112ae1.jpg");
        ucenterMember.setIsDisabled(false);
        baseMapper.insert(ucenterMember);
        return ResultObj.ok();
    }

    @Override
    public Integer countRegister(String day) {
        Integer count=baseMapper.countRegister(day);
        return count;
    }
}
